﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using com.google.zxing;
using com.google.zxing.common;
using System.Collections;

namespace 条形码解码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            myInit();
        }

        private void myInit()
        {
            //如下是设置文本框布局
            txtDecoder.Location = new Point(0, 0);
            txtDecoder.Width = splitContainer2.Panel2.Width-2;
            txtDecoder.Height = splitContainer2.Panel2.Height-2;

        }

        private void btnLoadPiature_Click(object sender, EventArgs e)
        {
            //加载图片
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                string strFileName = openFileDialog1.FileName;
                pictureBox1.Image = Image.FromFile(strFileName);
            }
        }

        private void txtDecoder_Resize(object sender, EventArgs e)
        {
            txtDecoder.Location = new Point(0, 0);
            txtDecoder.Width = splitContainer2.Panel2.Width-2;
            txtDecoder.Height = splitContainer2.Panel2.Height-2;
        }

        private void btnDecoder_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap bitmap = new Bitmap(pictureBox1.Image);

                LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);
                BinaryBitmap b = new BinaryBitmap(new HybridBinarizer(source));
                Hashtable hints = new Hashtable();
                Result result = new MultiFormatReader().decode(b, hints);

                txtDecoder.Text = result.Text;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("不能正确解码，原因是:" + ex.Message);
            	
            }

        }
    }
}
